<?php
/**
 * Created by PhpStorm OF Administrator.
 * User: YUYI
 * Date: 2018/7/23 0023
 * Time: 下午 2:05
 * Administrator
 */

namespace app\admin\controller;


use app\common\controller\Base;
use \app\common\model\Auths as AuthModel;
use app\common\model\Groups;
use think\db\Query;

class Group extends Base
{
    protected $getList = 'Groups';
    /**
     * 组别管理首页
     * @return mixed
     */
    public function index()
    {
        return $this->fetch();
    }

    /**
     * 查看列表详情
     * @param $id
     * @return mixed
     */
    public function detail($id)
    {
        $group = Groups::get($id);
        if (!$group){
            $this->error('非法访问');
        }
        $this->assign('group',$group);
        return $this->fetch();
    }

    /**
     * 获取组对应权限
     * @param $id
     * @return mixed
     */
    public function getGroupAuthids($id)
    {
        $group = Groups::get($id);
        if (!$group){
            $this->error('非法访问');
        }
        if ($group->authIds == 'all'){
            $where = null;
        }else{
            $where = ['id'=>['in',$group->authIds]];
        }
        $auths = AuthModel::all($where);
        $authss = AuthModel::all();
        $res = [];
        foreach ($authss as $auth1){
            $flag = false;
            foreach ($auths as $auth2){
                if ($auth1->id == $auth2->id){
                    $flag = true;
                    break;
                }
            }
            $res[] = ['name'=>$auth1->authName,'value'=>$auth1->id,"checked"=>$flag];
        }
        return json_decode($this->successInfo('成功',$res));
    }

    /**
     * 更新组权限
     * @return string
     */
    public function updateGroupAuths()
    {
        $id = input('id');
        if (!$id){
            return $this->errorInfo('非法访问');
        }
        $g = Groups::get($id);
        if (!$g){
            return $this->errorInfo('异常访问');
        }
        if (!input('?authids')){
            $ids = '0';
        }else{
            $ids = '';
            $authids = $this->request->isPost()?$_POST['authids']:$_GET['authids'];
            if (is_array($authids)){
                $ids = implode(',',$authids);
            }
        }
        $r = $g->save(['authIds'=>$ids]);
        if ($r===false){
            return $this->errorInfo('保存失败');
        }
        return $this->successInfo('成功');
    }

    /**
     * 新增组
     * @return mixed
     */
    public function add()
    {
        return $this->fetch();
    }

    /**
     * 新增保存
     */
    public function save()
    {
        $data = input();
        if (!$data['authids']){
            $data['authids'] = 0;
        }
        $g = Groups::create($data);
        if ($g){
            return $this->errorInfo('新增失败');
        }
        return $this->successInfo('成功');
    }

    /**
     * 更新状态
     * @param $id
     * @return string
     */
    public function updateStatus($id)
    {
        $status = input('status');
        if (!$status){
            return $this->errorInfo('非法');
        }
        $g = Groups::get($id);
        if (!$g){
            return $this->errorInfo('异常访问');
        }
        $r = $g->save(['status'=>$status]);
        if ($r===false){
            return $this->errorInfo('保存失败');
        }
        return $this->successInfo('成功');
    }
}